package com.example.touch.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.touch.R;
import com.example.touch.entity.Student;

import java.util.List;

public class StudentArrayAdapter extends ArrayAdapter<Student> {

   private int mResource;
   public StudentArrayAdapter(@NonNull Context context, int resource, @NonNull List<Student> objects) {
      super(context, resource, objects);
      mResource = resource;
   }

   @Nullable
   @Override
   public Student getItem(int position) {
      return super.getItem(position);
   }

   @NonNull
   @Override
   public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
      View view;
      Student student = getItem(position);
      ViewHolder viewHolder;
      if (convertView == null) {
         view = LayoutInflater.from(getContext()).inflate(mResource, parent, false);
         viewHolder = new ViewHolder();
         viewHolder.nameText = view.findViewById(R.id.text1);
         viewHolder.ageText = view.findViewById(R.id.text2);
         view.setTag(viewHolder);
         view.setTag(R.integer.tagkey, student);
      } else {
         view = convertView;
         viewHolder = (ViewHolder) view.getTag();
      }
      viewHolder.nameText.setText(student.getName());
      viewHolder.ageText.setText("" + student.getAge());

      return view;
   }

   private class ViewHolder {
      TextView nameText;
      TextView ageText;
   }
}
